/***********************************
 * @author David Buezas (david.buezas at gmail.com) Feel free to copy, modify and use the source as it fits you.
 * @modified Aksenov 14\06
 */
package ganga.custodian.sandbox;




import ganga.custodian.R;
import ganga.plot.KIGSeries;

import java.text.DecimalFormat;
import java.util.Vector;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.XYPlot;

public class plotActivity extends Activity  //implements OnTouchListener 
{
	private XYPlot mySimpleXYPlot;
	private SimpleXYSeries mySeries;
	private SimpleXYSeries aSeries;
	private KIGSeries kSeries;
	private LineAndPointFormatter  colorSeries;
	private PointF minXY;
	private PointF maxXY;
	private float absMinX;
	private float absMaxX;
	private float minNoError;
	private float maxNoError;
	private double minDif;
	private double d1[]=new double []{1,2,3,4,5,6,7,8,9,10};
	 
   	
	
	//array to vector
	public Vector<Double> arrayToVector(double array[])	{
		Vector<Double> localVector = new Vector<Double>();
		for(int i=0;i<array.length;i++){
			//double x=i;
			double y=array[i];
			//localVector.add(x);
			localVector.add(y);
		}
		return(localVector);
		
	}
	//part of array to vector
	public Vector<Double> partArrayToVector(double array[],int strt,int end ){ 
		int i;
		Vector<Double> localVector = new Vector<Double>();
		for(i=strt;i<=end;i++){
			//double x=i;
			double y=array[i];
			//localVector.add(x);
			localVector.add(y);
		}
		return localVector;
		
	}
	
	
	final private double difPadding = 0.1;
	
	
	public void Access_Button(View v){
		 EditText Edit1 = (EditText)findViewById(R.id.editText1);
		 EditText Edit2 = (EditText)findViewById(R.id.editText2);
		 int begin = Integer.parseInt(Edit1.getText().toString());
		 int end = Integer.parseInt(Edit2.getText().toString());
		 mySimpleXYPlot.removeSeries(mySeries, LineAndPointRenderer.class);
	  	 mySeries = new SimpleXYSeries(partArrayToVector(d1, begin, end));
	  	 mySimpleXYPlot.addSeries(mySeries, LineAndPointRenderer.class, colorSeries);
		 mySimpleXYPlot.redraw();
		 //calculateBorders(mySimpleXYPlot);
		
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.plot);
		setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
		mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
		buildXYPlot(mySimpleXYPlot);
		kSeries = new KIGSeries(arrayToVector(d1));
		 Paint lineFill = new Paint();
	        lineFill.setAlpha(400);
	        lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.BLUE, Shader.TileMode.MIRROR));
	       	colorSeries= new LineAndPointFormatter(Color.rgb(255, 200, 0), Color.rgb(200, 255, 0), Color.argb(100, 0,255, 100));
		  colorSeries.setFillPaint(lineFill);
		mySimpleXYPlot.addSeries(kSeries, LineAndPointRenderer.class, colorSeries);
		mySimpleXYPlot.redraw();
		mySimpleXYPlot.setRangeBoundaries(kSeries.getMinY()-5, kSeries.getMaxY()+5, BoundaryMode.FIXED);
		//calculateBorders(mySimpleXYPlot);

	}
	/*public void onDrawerOpened(){
		View drawerView = getLayoutInflater().inflate(R.layout.drawer, null);
		setContentView(drawerView);
		int width = getWindow().getAttributes().width, height = getWindow().getAttributes().height;
		LayoutParams params = new LayoutParams(width, height);
		getWindow().addContentView(drawerView, params);	
	}
	/*
	// Definition of the touch states
	static final private int NONE = 0;
	static final private int ONE_FINGER_DRAG = 1;
	static final private int TWO_FINGERS_DRAG = 2;
	private int mode = NONE;
	
	private PointF firstFinger;
	private float lastScrolling;
	private float distBetweenFingers;
	private float lastZooming;
	
		public boolean onTouch(View arg0, MotionEvent event) {
		switch(event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN: // Start gesture
				firstFinger = new PointF(event.getX(), event.getY());
				mode = ONE_FINGER_DRAG;
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_POINTER_UP:
				//When the gesture ends, a thread is created to give inertia to the scrolling and zoom 
				final Timer t = new Timer();
				t.schedule(new TimerTask() {
					@Override
					public void run() {
						while(Math.abs(lastScrolling) > 1f || Math.abs(lastZooming - 1) < 1.01) {
							lastScrolling *= .8;	//speed of scrolling damping
							scroll(lastScrolling);
							lastZooming += (1 - lastZooming) * .2;	//speed of zooming damping
							zoom(lastZooming);
							checkBoundaries();
							try {
								mySimpleXYPlot.postRedraw();
							} catch (final InterruptedException e) {
								e.printStackTrace();
							}
							// the thread lives until the scrolling and zooming are imperceptible
						}
					}
				}, 0);
				
			case MotionEvent.ACTION_POINTER_DOWN: // second finger
				distBetweenFingers = spacing(event);
				// the distance check is done to avoid false alarms
				if (distBetweenFingers > 5f)
					mode = TWO_FINGERS_DRAG;
				break;
			case MotionEvent.ACTION_MOVE:
				if (mode == ONE_FINGER_DRAG) {
					final PointF oldFirstFinger = firstFinger;
					firstFinger = new PointF(event.getX(), event.getY());
					lastScrolling = oldFirstFinger.x - firstFinger.x;
					scroll(lastScrolling);
					lastZooming = (firstFinger.y - oldFirstFinger.y) / mySimpleXYPlot.getHeight();
					if (lastZooming < 0)
						lastZooming = 1 / (1 - lastZooming);
					else
						lastZooming += 1;
					zoom(lastZooming);
					checkBoundaries();
					mySimpleXYPlot.redraw();
					
				} else if (mode == TWO_FINGERS_DRAG) {
					final float oldDist = distBetweenFingers;
					distBetweenFingers = spacing(event);
					lastZooming = oldDist / distBetweenFingers;
					zoom(lastZooming);
					checkBoundaries();
					mySimpleXYPlot.redraw();
				}
				break;
		}
		return true;
	}
	
	private void zoom(float scale) {
		final float domainSpan = maxXY.x - minXY.x;
		final float domainMidPoint = maxXY.x - domainSpan / 2.0f;
		final float offset = domainSpan * scale / 2.0f;
		minXY.x = domainMidPoint - offset;
		maxXY.x = domainMidPoint + offset;
	}
	
	private void scroll(float pan) {
		final float domainSpan = maxXY.x - minXY.x;
		final float step = domainSpan / mySimpleXYPlot.getWidth();
		final float offset = pan * step;
		minXY.x += offset;
		maxXY.x += offset;
	}
	
	private float spacing(MotionEvent event) {
		final float x = event.getX(0) - event.getX(1);
		final float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}
	
	private void checkBoundaries() {
		//Make sure the proposed domain boundaries will not cause plotting issues
		if (minXY.x < absMinX)
			minXY.x = absMinX;
		else if (minXY.x > maxNoError)
			minXY.x = maxNoError;
		if (maxXY.x > absMaxX)
			maxXY.x = absMaxX;
		else if (maxXY.x < minNoError)
			maxXY.x = minNoError;
		if (maxXY.x - minXY.x < minDif)
			maxXY.x = maxXY.x + (float) (minDif - (maxXY.x - minXY.x));
		mySimpleXYPlot.setDomainBoundaries(minXY.x, maxXY.x, BoundaryMode.AUTO);
	}
	private void calculateBorders(XYPlot aSimpleXYPlot) {
		//---It Procedure
			aSimpleXYPlot.calculateMinMaxVals();
			minXY = new PointF(aSimpleXYPlot.getCalculatedMinX().floatValue(),
					aSimpleXYPlot.getCalculatedMinY().floatValue()); //initial minimum data point
			absMinX = minXY.x; //absolute minimum data point
			//absolute minimum value for the domain boundary maximum
			minNoError = Math.round(mySeries.getX(1).floatValue() + 2);
			maxXY = new PointF(aSimpleXYPlot.getCalculatedMaxX().floatValue(),
					aSimpleXYPlot.getCalculatedMaxY().floatValue()); //initial maximum data point
			absMaxX = maxXY.x; //absolute maximum data point
			//absolute maximum value for the domain boundary minimum
			maxNoError = (float) Math.round(mySeries.getX(mySeries.size() - 1).floatValue()) - 2;
			
			//Check x data to find the minimum difference between two neighboring domain values
			//Will use to prevent zooming further in than this distance
			double temp1 = mySeries.getX(0).doubleValue();
			double temp2 = mySeries.getX(1).doubleValue();
			double temp3;
			double thisDif;
			minDif = 1000000;	//increase if necessary for domain values
			for (int i = 2; i < mySeries.size(); i++) {
				temp3 = mySeries.getX(i).doubleValue();
				thisDif = Math.abs(temp1 - temp3);
				if (thisDif < minDif)
					minDif = thisDif;
				temp1 = temp2;
				temp2 = temp3;
			}
			minDif = minDif + difPadding; //with padding, the minimum difference
		//---	

	
}
*/
	private void buildXYPlot(XYPlot aSimpleXYPlot) {
//		aSimpleXYPlot.setOnTouchListener(this);
		
		//Plot layout configurations
		aSimpleXYPlot.getGraphWidget().setTicksPerRangeLabel(1);
		aSimpleXYPlot.getGraphWidget().setTicksPerDomainLabel(1);
		aSimpleXYPlot.getGraphWidget().setRangeValueFormat(
				new DecimalFormat("#####.##"));
		aSimpleXYPlot.getGraphWidget().setDomainValueFormat(
				new DecimalFormat("#####.##"));
		aSimpleXYPlot.getGraphWidget().setRangeLabelWidth(25);
		aSimpleXYPlot.setRangeLabel("");
		aSimpleXYPlot.setDomainLabel("");
		aSimpleXYPlot.disableAllMarkup();
	}
	
	}//plotActivity

//private class  DrawerToggler implements OnClickListener {
//}
	
